local filename = "../webservice/matamatajogos"
local imprime = false

function redraw()
    file, err = io.open(filename, "r")
    if (err == nil) then
        canvas:attrFont('vera',25,'bold')
        local i = 0
        for line in file:lines() do
            print ("****" .. line)
            time1, value1, value2, time2, pen1, pen2 = string.match(line, "^([a-z-]*)[ ]+([0-9]*)[ ]+([0-9]*)[ ]+([a-z-]*)[ ]*([0-9]*)[ ]*([0-9]*)$")
            matamataTela = string.match(line, "^([a-zA-Z12]*)$")
            if (matamataTela ~= nil and matamataTela == 'mataMata1') then imprime = true elseif (matamataTela ~= nil and matamataTela ~= tela) then imprime = false end
            if (imprime and time1 ~= nil) then
                local img1 = canvas:new(("../media/bandeiras-legendas/" .. time1 .. ".png"))
                local img2 = canvas:new(("../media/bandeiras-legendas/" .. time2 .. ".png"))
                if i < 4 then
                    canvas:compose(14, (63 + i*100), img1)
                    canvas:compose(14, (63 + 48 + i*100), img2)
                    canvas:drawText(133, (64 + i*100), value1)
                    canvas:drawText(133, (64 + 47 + i*100), value2)
                    if (pen1 ~= null and pen1 ~= "") then
                        canvas:drawText(151, (65 + i*99), '[' .. pen1 .. ']*')
                        canvas:drawText(151, (65 + 47 + i*99), '[' .. pen2 .. ']*')
                    end
                elseif i > 3 and i < 6 then
                    canvas:compose(226, (86 + (i - 4)*206), img1)
                    canvas:compose(226, (86 + 102 + (i - 4)*207), img2)
                    canvas:drawText(340, (88 + (i - 4)*204), value1)
                    canvas:drawText(340, (88 + 102 + (i - 4)*207), value2)
                    if (pen1 ~= null and pen1 ~= "") then
                        canvas:drawText(356, (88 + (i - 4)*204), '[' .. pen1 .. ']*')
                        canvas:drawText(356, (88 + 102 + (i - 4)*207), '[' .. pen2 .. ']*')
                    end
                elseif i == 6 then
                    canvas:compose(433, 135, img1)
                    canvas:compose(433, 349, img2)
                    canvas:drawText(555, 137, value1)
                    canvas:drawText(555, 351, value2)
                    if (pen1 ~= null and pen1 ~= "") then
                        canvas:drawText(573, 137, '[' .. pen1 .. ']*')
                        canvas:drawText(573, 351, '[' .. pen2 .. ']*')
                    end
                elseif i == 7 then
                    canvas:compose(646, 245, img1)
                    canvas:drawText(760, 247, value1)
                    if (pen1 ~= null and pen1 ~= "") then
                        canvas:drawText(778, 247, '[' .. pen1 .. ']*')
                    end
                elseif i == 8 then
                    canvas:compose(646, 376, img1)
                    canvas:compose(646, 423, img2)
                    canvas:drawText(761, 377, value1)
                    canvas:drawText(761, 423, value2)
                    if (pen1 ~= null and pen1 ~= "") then
                        canvas:drawText(779, 377, '[' .. pen1 .. ']*')
                        canvas:drawText(779, 423, '[' .. pen2 .. ']*')
                    end
                end
                i = i + 1
                canvas:flush()                    
            end
        end
        file:close()
    else 
        print (err)
    end
end

redraw()
